package com.namecard.namecardapi.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.namecard.namecardapi.entity.AccountInfo;
import com.namecard.namecardapi.entity.CardInfo;
import com.namecard.namecardapi.entity.RechargeInfo;
import com.namecard.namecardapi.entity.emums.RaiseExposureEnum;
import com.namecard.namecardapi.entity.vo.UserAccountVO;
import com.namecard.namecardapi.exception.BusinessException;
import com.namecard.namecardapi.mapper.AccountInfoMapper;
import com.namecard.namecardapi.mapper.CardInfoMapper;
import com.namecard.namecardapi.mapper.RechargeInfoMapper;
import com.namecard.namecardapi.service.AccountInfoService;
import com.namecard.namecardapi.util.Constants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wzx
 * @since 2023-07-03
 */

@Service
@DS("slave")
public class AccountInfoServiceImpl extends ServiceImpl<AccountInfoMapper, AccountInfo> implements AccountInfoService {

    @Resource
    AccountInfoMapper accountInfoMapper;

    @Resource
    RechargeInfoMapper rechargeInfoMapper;

    @Resource
    CardInfoMapper cardInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int recharge(BigDecimal piNum, String userId) {
        // 先找到原来的已经充值的数据
        RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(new QueryWrapper<RechargeInfo>().eq("user_id", userId).select("pi_num", "point_balance"));
        // 更新用户的充值总量数据
        int update1 = rechargeInfoMapper.update(null, new UpdateWrapper<RechargeInfo>()
                .eq("user_id", userId)
                .set("pi_num", rechargeInfo.getPiNum().subtract(piNum))
                .set("point_balance", rechargeInfo.getPointBalance().add(piNum.multiply(Constants.PI_RANGE_POINT)))
                .set("create_time", null));
        // 先找到原来的账号数据
        AccountInfo accountInfo = accountInfoMapper.selectOneAccount(userId);
        // 更新用户的账号数据
        int update = accountInfoMapper.update(null, new UpdateWrapper<AccountInfo>()
                .eq("user_id", userId)
                .set("pi_balance", accountInfo.getPiBalance().subtract(piNum))
                .set("point_balance", accountInfo.getPointBalance().add(piNum.multiply(Constants.PI_RANGE_POINT))));
        return update1 == update && update != 0 ? 1 : -1;
    }

    @Override
    public void raiseExposure(Integer methodId, String userId) {
        // 获取积分方案对应的数据
        RaiseExposureEnum raiseExposureEnum;
        switch (methodId){
            case 1 : raiseExposureEnum = RaiseExposureEnum.METHOD_1;break;
            case 2 : raiseExposureEnum = RaiseExposureEnum.METHOD_2;break;
            case 3 : raiseExposureEnum = RaiseExposureEnum.METHOD_3;break;
            case 4 : raiseExposureEnum = RaiseExposureEnum.METHOD_4;break;
            case 5 : raiseExposureEnum = RaiseExposureEnum.METHOD_5;break;
            case 6 : raiseExposureEnum = RaiseExposureEnum.METHOD_6;break;
            default: throw new BusinessException("没有该类商品，请联系后台");
        }
        // 兑换的曝光值
        Long raiseExposure = raiseExposureEnum.getRaiseExposure();
        // 需要的积分数据
        BigDecimal needPoint = raiseExposureEnum.getNeedPoint();
        UserAccountVO userAccountVO = accountInfoMapper.selectBalanceData(userId);
        if (userAccountVO.getAvailableBalance().compareTo(needPoint) < 0) {
            throw new BusinessException("积分不足");
        }
        BigDecimal newAvailableBalance = userAccountVO.getAvailableBalance().subtract(needPoint);
        BigDecimal newPointBalance = userAccountVO.getPointBalance().subtract(needPoint);

        // 查询已有的曝光率
        CardInfo cardInfo = cardInfoMapper.selectOne(new QueryWrapper<CardInfo>().eq("user_id", userId)
                .select("exposure_value"));
        // 修改曝光率值
        int update = cardInfoMapper.update(null, new UpdateWrapper<CardInfo>().eq("user_id", userId)
                .set("exposure_value", cardInfo.getExposureValue() + raiseExposure));
        // 修改剩余积分值
        int update1 = accountInfoMapper.update(null,new UpdateWrapper<AccountInfo>().eq("uid",userId)
                                                                            .set("point_balance",newPointBalance)
                                                                            .set("available_balance",newAvailableBalance));
        if (update1 + update < 2){
            throw new BusinessException("兑换失败，请联系后台人员");
        }
    }

    @Override
    public UserAccountVO getUserAccountInfo(String userId) {
        try {
            // 获取两个余额数据
            return accountInfoMapper.selectBalanceData(userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
